<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>翻转二叉树 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-block pre {
            padding: 1.5rem;
            overflow-x: auto;
        }
        .keyword { color: #c678dd; }
        .function { color: #61afef; }
        .string { color: #98c379; }
        .comment { color: #5c6370; font-style: italic; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .complexity-badge {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 9999px;
            font-size: 0.875rem;
            font-weight: 500;
            display: inline-block;
            margin: 0.25rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-4">
                <i class="fas fa-code-branch mr-4"></i>翻转二叉树
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                探索递归的优雅之美，用最简洁的代码实现二叉树的镜像翻转
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Problem Description Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-purple-100 rounded-full p-3 mr-4">
                    <i class="fas fa-book-open text-purple-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed">
                <span class="drop-cap">翻</span>转一棵二叉树，即将一棵二叉树的所有节点的左右子树进行交换。这是一道经典的二叉树操作题目，考察对递归思想的理解和应用。
            </p>
            <div class="mt-6 bg-gray-50 rounded-xl p-6">
                <p class="text-gray-600 mb-2"><i class="fas fa-lightbulb text-yellow-500 mr-2"></i><strong>示例：</strong></p>
                <div class="flex items-center justify-center space-x-8">
                    <div class="text-center">
                        <p class="text-sm text-gray-500 mb-2">输入</p>
                        <code class="bg-gray-200 px-3 py-1 rounded">[4,2,7,1,3,6,9]</code>
                    </div>
                    <i class="fas fa-arrow-right text-purple-500 text-2xl"></i>
                    <div class="text-center">
                        <p class="text-sm text-gray-500 mb-2">输出</p>
                        <code class="bg-gray-200 px-3 py-1 rounded">[4,7,2,9,6,3,1]</code>
                    </div>
                </div>
            </div>
        </div>

        <!-- Visual Representation -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-blue-100 rounded-full p-3 mr-4">
                    <i class="fas fa-project-diagram text-blue-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">可视化演示</h2>
            </div>
            <div class="mermaid">
                graph TD
                    subgraph "原始二叉树"
                        A1[4] --> B1[2]
                        A1 --> C1[7]
                        B1 --> D1[1]
                        B1 --> E1[3]
                        C1 --> F1[6]
                        C1 --> G1[9]
                    end
                    
                    subgraph "翻转后的二叉树"
                        A2[4] --> B2[7]
                        A2 --> C2[2]
                        B2 --> D2[9]
                        B2 --> E2[6]
                        C2 --> F2[3]
                        C2 --> G2[1]
                    end
                    
                    style A1 fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style A2 fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-green-100 rounded-full p-3 mr-4">
                        <i class="fas fa-brain text-green-600 text-2xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">核心考点</h3>
                </div>
                <div class="space-y-4">
                    <div class="flex items-center">
                        <i class="fas fa-sync-alt text-purple-500 mr-3"></i>
                        <span class="text-lg">递归思想</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-sitemap text-blue-500 mr-3"></i>
                        <span class="text-lg">深度优先搜索 (DFS)</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-tree text-green-500 mr-3"></i>
                        <span class="text-lg">二叉树遍历</span>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-orange-100 rounded-full p-3 mr-4">
                        <i class="fas fa-clock text-orange-600 text-2xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">复杂度分析</h3>
                </div>
                <div class="space-y-4">
                    <div>
                        <span class="complexity-badge">
                            <i class="fas fa-hourglass-half mr-2"></i>时间复杂度：O(n)
                        </span>
                        <p class="text-gray-600 mt-2">需要访问每个节点一次</p>
                    </div>
                    <div>
                        <span class="complexity-badge">
                            <i class="fas fa-memory mr-2"></i>空间复杂度：O(h)
                        </span>
                        <p class="text-gray-600 mt-2">递归调用栈的深度为树高</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Solution Approach -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-indigo-100 rounded-full p-3 mr-4">
                    <i class="fas fa-lightbulb text-indigo-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">解题思路</h2>
            </div>
            <div class="prose prose-lg max-w-none">
                <p class="text-gray-700 leading-relaxed mb-6">
                    翻转二叉树的核心思想是<strong>递归地交换每个节点的左右子节点</strong>。这个过程可以分解为以下步骤：
                </p>
                <ol class="space-y-4">
                    <li class="flex items-start">
                        <span class="bg-purple-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">1</span>
                        <div>
                            <strong>基础情况</strong>：如果当前节点为空，直接返回 None
                        </div>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-purple-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">2</span>
                        <div>
                            <strong>递归翻转</strong>：递归地翻转左子树和右子树
                        </div>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-purple-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">3</span>
                        <div>
                            <strong>交换子节点</strong>：将当前节点的左右子节点进行交换
                        </div>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-purple-500 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3 flex-shrink-0">4</span>
                        <div>
                            <strong>返回根节点</strong>：返回处理后的根节点
                        </div>
                    </li>
                </ol>
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-red-100 rounded-full p-3 mr-4">
                    <i class="fas fa-code text-red-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">示例代码</h2>
            </div>
            <div class="code-block">
                <pre><code><span class="keyword">def</span> <span class="function">invertTree</span>(root):
    <span class="comment"># 基础情况：空节点直接返回</span>
    <span class="keyword">if not</span> root:
        <span class="keyword">return None</span>
    
    <span class="comment"># 递归翻转左右子树，并交换位置</span>
    root.left, root.right = <span class="function">invertTree</span>(root.right), <span class="function">invertTree</span>(root.left)
    
    <span class="comment"># 返回翻转后的根节点</span>
    <span class="keyword">return</span> root</code></pre>
            </div>
            <div class="mt-6 bg-blue-50 rounded-xl p-6">
                <p class="text-blue-800">
                    <i class="fas fa-info-circle mr-2"></i>
                    <strong>代码亮点</strong>：使用 Python 的多重赋值特性，一行代码完成子节点交换和递归调用，体现了代码的简洁性和优雅性。
                </p>
            </div>
        </div>
    </main>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            themeVariables: {
                primaryColor: '#667eea',
                primaryTextColor: '#fff',
                primaryBorderColor: '#764ba2',
                lineColor: '#5a67d8',
                secondaryColor: '#764ba2',
                tertiaryColor: '#fff'
            }
        });
    </script>
</body>
</html>